﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Entity;

public partial class admin_FriendLinks : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView("");
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    /// <param name="sqlwhere">条件</param>
    /// <param name="sort">排序</param>
    /// <param name="sort"></param>
    private void BindGridView(string sqlwhere)
    {
        ViewState["strWhere"] = sqlwhere;//条件
        ///根据权限加载
        int recordCount = 0;
        recordCount = LinksDAL.Instance.SelectLinksCount(sqlwhere);//根据条件，获取信息的条数
        SysUserInfoPage.RecordCount = recordCount;//分页赋值
        List<Links> list = LinksDAL.Instance.SelectLinksInfo(SysUserInfoPage.PageSize, SysUserInfoPage.CurrentPageIndex, sqlwhere);
        grdSysUserInfo.DataSource = list;
        grdSysUserInfo.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (txtKey.Text.Trim() != "")
        {
            BindGridView(" title like '%" + txtKey.Text.Trim() + "%' or entitle like '%" + txtKey.Text.Trim() + "%' or url  like '%" + txtKey.Text.Trim() + "%' ");
        }
        else
        {
            BindGridView("");
        }
    }


    /// <summary>
    /// 数据绑定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grdSysUserInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Attributes.Add("onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#F3FFE3'}");//当鼠标停留时更改背景色
            //e.Row.Attributes.Add("onmouseout", "if(this!=prevselitem){this.style.backgroundColor='#FFFFFF'}");//当鼠标移开时还原背景色
            //e.Row.Attributes.Add("onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)");//点击行变色

            e.Row.Attributes["style"] = "Cursor:hand"; //设置悬浮鼠标指针形状为"小手"
            Label lblTemp = (Label)e.Row.FindControl("lblNum");
            lblTemp.Text = (e.Row.RowIndex + 1).ToString();
            // + SysUserInfoPage.PageSize * (SysUserInfoPage.CurrentPageIndex - 1)).ToString();

            //Label lblUserName = (Label)e.Row.FindControl("lblUserName");
           
            HiddenField hfID = (HiddenField)e.Row.FindControl("hfID");
            
            LinkButton lnkMod = (LinkButton)e.Row.FindControl("lnkMod");
            //修改用户
            if (lnkMod != null)
            {
                lnkMod.CommandArgument = hfID.Value;
                lnkMod.PostBackUrl = "FriendLinksMod.aspx?id=" + hfID.Value;
            }

            LinkButton lnkDel = (LinkButton)e.Row.FindControl("lnkDel");
            //修改用户
            if (lnkDel != null)
            {
                lnkDel.CommandArgument = hfID.Value;
            }
        }

    }

    #region 分页
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ChangePage(object sender, EventArgs e)
    {
        BindGridView(ViewState["strWhere"].ToString());
    }
    #endregion
    protected void grdSysUserInfo_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "lnkMod")
        {
            string id = e.CommandArgument.ToString();
           
        }
        else if (e.CommandName == "lnkDel")
        {
           
            if (LinksDAL.Instance.DeleteLinksForId(e.CommandArgument.ToString()))
            {
                Alert( "删除成功！");
                BindGridView(ViewState["strWhere"].ToString());
            }
            else
            {
                Alert("删除失败，请重新操作！");
            }
            
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        txtKey.Text = "";
        BindGridView("");
    }
}